import { Path, Rect, Segment, Vec2 } from '@clipper/geometry'
import { Mat3 } from '@clipper/matrix'
import { bgCanvas } from '@clipper/ui'
import { getConvexHull, getConvexHull2 } from '@clipper/convex'
import {
  minimalCoverRect,
  minimalCoverParallelogram
} from '@clipper/minimal_conver'

let W = bgCanvas.width
let H = bgCanvas.height

function transformPath (path: Path, mat: Mat3) {
  let newPath = new Path()
  for (const v of path.points) {
    newPath.points.push(mat.applyToVec(v))
  }
  return newPath
}

function randomPoints (
  w: number,
  h: number,
  n: number,
  bx: number = 0,
  by: number = 0
) {
  let points: Vec2[] = []
  for (let i = 0; i < n; i++) {
    let x = bx + Math.round(w * Math.random())
    let y = by + Math.round(h * Math.random())
    points.push(new Vec2(x, y))
  }
  return points
}

let points = randomPoints(500, 400, 100, 100, 100)

let firstThree = [points[0], points[1], points[2]]
let firstThreePath = new Path()
firstThreePath.points = firstThree

let origPath = new Path()
origPath.points = points

let convexPoints = getConvexHull2(points)
let convexPath = new Path()
convexPath.points = convexPoints
console.log(origPath.toString())
console.log(convexPath.toString())

bgCanvas.drawPoints(points, 'red')
bgCanvas.drawPath(convexPath, { strokeColor: 'blue', close: false })
bgCanvas.drawPath(firstThreePath, { strokeColor: 'red', close: true })
bgCanvas.drawPoint(points[points.length - 1], 'green')

// 最小覆盖矩形
// let rectPoints = minimalCoverRect(convexPoints)
// let rectPath = new Path()
// rectPath.points = rectPoints
// bgCanvas.drawPath(rectPath, { strokeColor: 'green', close: true })

// 最小覆盖平四边形
// let paragram = minimalCoverParallelogram(convexPoints)
// let paragramPoints = [paragram!.a, paragram!.b, paragram!.c, paragram!.d]
// let paragramPath = new Path()
// paragramPath.points = paragramPoints
// bgCanvas.drawPath(paragramPath, { strokeColor: 'red', close: true })
